package com.dark.config;

import com.dark.interceptor.LoginInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.util.Arrays;

@Slf4j
@Configuration
@EnableConfigurationProperties({SsoProperties.class})
public class WebMvcConfig implements WebMvcConfigurer {

    @Resource
    private SsoProperties jjcLimitProperties;
    /**
     * 登录拦截器
     */
    @Resource
    private LoginInterceptor loginInterceptor;
    /**
     * 登录拦截降级开关
     */
    @Value("${flower-falling.login.check.enable:true}")
    private boolean enableLoginCheck;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        if (!enableLoginCheck) {
            return;
        }

        String[] excludePaths = jjcLimitProperties.getExclude().getPaths();
        log.info("addInterceptors config excludePaths={}", Arrays.toString(excludePaths));
        //指定需要配置的拦截器
        registry.addInterceptor(loginInterceptor)
                //指定需要拦截的请求  /**  表示拦截所有
                .addPathPatterns("/**")
                //指定需要排除的请求
                .excludePathPatterns(excludePaths);
    }
}
